package com.government.tag.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.government.common.core.domain.BaseTreeVo;
import com.government.common.utils.DateUtils;
import com.government.common.utils.TreeUtils;
import com.government.tag.vo.TagTypeVO;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.government.tag.mapper.TagTypeMapper;
import com.government.tag.domain.TagType;
import com.government.tag.service.ITagTypeService;

/**
 * 标签类型Service业务层处理
 *
 * @author system
 * @date 2023-09-13
 */
@Service
public class TagTypeServiceImpl implements ITagTypeService {
    @Autowired
    private TagTypeMapper tagTypeMapper;

    /**
     * 查询标签类型
     *
     * @param tagTypeId 标签类型主键
     * @return 标签类型
     */
    @Override
    public TagType selectTagTypeByTagTypeId(Long tagTypeId) {
        return tagTypeMapper.selectTagTypeByTagTypeId(tagTypeId);
    }

    /**
     * 查询标签类型列表
     *
     * @param tagType 标签类型
     * @return 标签类型
     */
    @Override
    public List<TagTypeVO> selectTagTypeList(TagType tagType) {
        return tagTypeMapper.selectTagTypeList(tagType);
    }

    /**
     * 新增标签类型
     *
     * @param tagType 标签类型
     * @return 结果
     */
    @Override
    public int insertTagType(TagType tagType) {
        tagType.setCreateTime(DateUtils.getNowDate());
        return tagTypeMapper.insertTagType(tagType);
    }

    /**
     * 修改标签类型
     *
     * @param tagType 标签类型
     * @return 结果
     */
    @Override
    public int updateTagType(TagType tagType) {
        tagType.setUpdateTime(DateUtils.getNowDate());
        return tagTypeMapper.updateTagType(tagType);
    }

    /**
     * 批量删除标签类型
     *
     * @param tagTypeIds 需要删除的标签类型主键
     * @return 结果
     */
    @Override
    public int deleteTagTypeByTagTypeIds(Long[] tagTypeIds) {
        return tagTypeMapper.deleteTagTypeByTagTypeIds(tagTypeIds);
    }

    /**
     * 删除标签类型信息
     *
     * @param tagTypeId 标签类型主键
     * @return 结果
     */
    @Override
    public int deleteTagTypeByTagTypeId(Long tagTypeId) {
        return tagTypeMapper.deleteTagTypeByTagTypeId(tagTypeId);
    }

    /**
     * 查询一级标签类型下拉列表
     *
     * @return 标签类型
     */
    @Override
    public List<TagType> selectFirstTagTypeList() {
        return tagTypeMapper.selectList(Wrappers.<TagType>lambdaQuery().isNull(TagType::getParentId));
    }

    /**
     * 查询标签类型树结构信息
     *
     * @return 部门树信息集合
     */
    @Override
    public List<BaseTreeVo> selectTagTypeTreeList() {

        List<TagType> tagTypeList = tagTypeMapper.selectList(Wrappers.<TagType>lambdaQuery());
        if (CollectionUtils.isEmpty(tagTypeList)) {

            return null;
        }
        HashMap<Long, List<BaseTreeVo>> allNodeMap = new HashMap<>();

        for (TagType item : tagTypeList) {
            Long parentId = Objects.isNull(item.getParentId()) ? 0 : item.getParentId();
            List<BaseTreeVo> nodeList = allNodeMap.get(parentId);
            if (CollectionUtils.isEmpty(nodeList)) {

                nodeList = Lists.newArrayList();
            }
            BaseTreeVo tagTypeTreeVO = new BaseTreeVo();
            tagTypeTreeVO.setId(String.valueOf(item.getTagTypeId()));
            tagTypeTreeVO.setValue(item.getTagTypeId());
            tagTypeTreeVO.setLabel(item.getName());
            nodeList.add(tagTypeTreeVO);
            allNodeMap.put(parentId, nodeList);
        }
        List<BaseTreeVo> rootNode = allNodeMap.get(0L);

        return TreeUtils.buildTree(rootNode, allNodeMap);
    }


}
